﻿/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#ifndef __6FC9EE51_F84F_48ba_B55D_EDCE4CC7D087__
#define __6FC9EE51_F84F_48ba_B55D_EDCE4CC7D087__

/////////////////////////////////////////////////////////////////////////////////////////////////////

namespace xos_common
{
    class des 
    {
        typedef bool (*PSubKey)[16][48];

    public:
        des();
        ~des();

    protected:
        bool SubKey[2][16][48]; // 两个密钥的16轮子密钥 
        char Tmp[256];          // 
        char deskey[16];        // 
        bool m_bM[64];

    protected:
        //通用置换函数 
        void Transform(bool *Out, bool *In, const char *Table, int len);

        //异或运算 
        void Xor(bool *InA, const bool *InB, int len);

        //循环左移 
        void MoveLeft(bool *In, int len, int loop);

        //字节转换成位 
        void Byte2Bit(bool *Out, const char *In, int bits);

        //位转换字节 
        void Bit2Byte(char *Out, const bool *In, int bits);

        //S 盒置换
        void funS(bool Out[32], const bool In[48]);

        //F 函数
        void funF(bool In[32], const bool Ki[48]);

        //生成子密钥
        void MakeSubKey(PSubKey pSubKey, const char Key[8]);

        //生成密钥 
        void MakeKey(const char* Key, int len);

        //一重DES加/解密 
        void DES(char Out[8], const char In[8], const PSubKey pSubKey, bool bEncode );

    public:
        int DoDES( char * Out, const char * In, long datalen, const char * Key, int keylen, bool bEncode );

    };
} // xos_common

#endif // __6FC9EE51_F84F_48ba_B55D_EDCE4CC7D087__
